package com.taobao.android.mnndemo;

public class Dsnt {
    static double X[][] = {
            {-0.947368,-0.842105,-0.736842,-0.631579,-0.526316,-0.421053,-0.315789,-0.210526,-0.105263,0,0.105263,0.210526,0.315789,0.421053,0.526316,0.631579,0.736842,0.842105,0.947368},
            {-0.947368,-0.842105,-0.736842,-0.631579,-0.526316,-0.421053,-0.315789,-0.210526,-0.105263,0,0.105263,0.210526,0.315789,0.421053,0.526316,0.631579,0.736842,0.842105,0.947368},
            {-0.947368,-0.842105,-0.736842,-0.631579,-0.526316,-0.421053,-0.315789,-0.210526,-0.105263,0,0.105263,0.210526,0.315789,0.421053,0.526316,0.631579,0.736842,0.842105,0.947368},
            {-0.947368,-0.842105,-0.736842,-0.631579,-0.526316,-0.421053,-0.315789,-0.210526,-0.105263,0,0.105263,0.210526,0.315789,0.421053,0.526316,0.631579,0.736842,0.842105,0.947368},
            {-0.947368,-0.842105,-0.736842,-0.631579,-0.526316,-0.421053,-0.315789,-0.210526,-0.105263,0,0.105263,0.210526,0.315789,0.421053,0.526316,0.631579,0.736842,0.842105,0.947368},
            {-0.947368,-0.842105,-0.736842,-0.631579,-0.526316,-0.421053,-0.315789,-0.210526,-0.105263,0,0.105263,0.210526,0.315789,0.421053,0.526316,0.631579,0.736842,0.842105,0.947368},
            {-0.947368,-0.842105,-0.736842,-0.631579,-0.526316,-0.421053,-0.315789,-0.210526,-0.105263,0,0.105263,0.210526,0.315789,0.421053,0.526316,0.631579,0.736842,0.842105,0.947368},
            {-0.947368,-0.842105,-0.736842,-0.631579,-0.526316,-0.421053,-0.315789,-0.210526,-0.105263,0,0.105263,0.210526,0.315789,0.421053,0.526316,0.631579,0.736842,0.842105,0.947368},
            {-0.947368,-0.842105,-0.736842,-0.631579,-0.526316,-0.421053,-0.315789,-0.210526,-0.105263,0,0.105263,0.210526,0.315789,0.421053,0.526316,0.631579,0.736842,0.842105,0.947368},
            {-0.947368,-0.842105,-0.736842,-0.631579,-0.526316,-0.421053,-0.315789,-0.210526,-0.105263,0,0.105263,0.210526,0.315789,0.421053,0.526316,0.631579,0.736842,0.842105,0.947368},
            {-0.947368,-0.842105,-0.736842,-0.631579,-0.526316,-0.421053,-0.315789,-0.210526,-0.105263,0,0.105263,0.210526,0.315789,0.421053,0.526316,0.631579,0.736842,0.842105,0.947368},
            {-0.947368,-0.842105,-0.736842,-0.631579,-0.526316,-0.421053,-0.315789,-0.210526,-0.105263,0,0.105263,0.210526,0.315789,0.421053,0.526316,0.631579,0.736842,0.842105,0.947368},
            {-0.947368,-0.842105,-0.736842,-0.631579,-0.526316,-0.421053,-0.315789,-0.210526,-0.105263,0,0.105263,0.210526,0.315789,0.421053,0.526316,0.631579,0.736842,0.842105,0.947368},
            {-0.947368,-0.842105,-0.736842,-0.631579,-0.526316,-0.421053,-0.315789,-0.210526,-0.105263,0,0.105263,0.210526,0.315789,0.421053,0.526316,0.631579,0.736842,0.842105,0.947368},
            {-0.947368,-0.842105,-0.736842,-0.631579,-0.526316,-0.421053,-0.315789,-0.210526,-0.105263,0,0.105263,0.210526,0.315789,0.421053,0.526316,0.631579,0.736842,0.842105,0.947368},
            {-0.947368,-0.842105,-0.736842,-0.631579,-0.526316,-0.421053,-0.315789,-0.210526,-0.105263,0,0.105263,0.210526,0.315789,0.421053,0.526316,0.631579,0.736842,0.842105,0.947368},
            {-0.947368,-0.842105,-0.736842,-0.631579,-0.526316,-0.421053,-0.315789,-0.210526,-0.105263,0,0.105263,0.210526,0.315789,0.421053,0.526316,0.631579,0.736842,0.842105,0.947368},
            {-0.947368,-0.842105,-0.736842,-0.631579,-0.526316,-0.421053,-0.315789,-0.210526,-0.105263,0,0.105263,0.210526,0.315789,0.421053,0.526316,0.631579,0.736842,0.842105,0.947368},
            {-0.947368,-0.842105,-0.736842,-0.631579,-0.526316,-0.421053,-0.315789,-0.210526,-0.105263,0,0.105263,0.210526,0.315789,0.421053,0.526316,0.631579,0.736842,0.842105,0.947368},
            {-0.947368,-0.842105,-0.736842,-0.631579,-0.526316,-0.421053,-0.315789,-0.210526,-0.105263,0,0.105263,0.210526,0.315789,0.421053,0.526316,0.631579,0.736842,0.842105,0.947368},
            {-0.947368,-0.842105,-0.736842,-0.631579,-0.526316,-0.421053,-0.315789,-0.210526,-0.105263,0,0.105263,0.210526,0.315789,0.421053,0.526316,0.631579,0.736842,0.842105,0.947368},
            {-0.947368,-0.842105,-0.736842,-0.631579,-0.526316,-0.421053,-0.315789,-0.210526,-0.105263,0,0.105263,0.210526,0.315789,0.421053,0.526316,0.631579,0.736842,0.842105,0.947368},
            {-0.947368,-0.842105,-0.736842,-0.631579,-0.526316,-0.421053,-0.315789,-0.210526,-0.105263,0,0.105263,0.210526,0.315789,0.421053,0.526316,0.631579,0.736842,0.842105,0.947368},
            {-0.947368,-0.842105,-0.736842,-0.631579,-0.526316,-0.421053,-0.315789,-0.210526,-0.105263,0,0.105263,0.210526,0.315789,0.421053,0.526316,0.631579,0.736842,0.842105,0.947368},
            {-0.947368,-0.842105,-0.736842,-0.631579,-0.526316,-0.421053,-0.315789,-0.210526,-0.105263,0,0.105263,0.210526,0.315789,0.421053,0.526316,0.631579,0.736842,0.842105,0.947368}
    };

    static double Y[][] = {
            {-0.96,-0.96,-0.96,-0.96,-0.96,-0.96,-0.96,-0.96,-0.96,-0.96,-0.96,-0.96,-0.96,-0.96,-0.96,-0.96,-0.96,-0.96,-0.96},
            {-0.88,-0.88,-0.88,-0.88,-0.88,-0.88,-0.88,-0.88,-0.88,-0.88,-0.88,-0.88,-0.88,-0.88,-0.88,-0.88,-0.88,-0.88,-0.88},
            {-0.8,-0.8,-0.8,-0.8,-0.8,-0.8,-0.8,-0.8,-0.8,-0.8,-0.8,-0.8,-0.8,-0.8,-0.8,-0.8,-0.8,-0.8,-0.8},
            {-0.72,-0.72,-0.72,-0.72,-0.72,-0.72,-0.72,-0.72,-0.72,-0.72,-0.72,-0.72,-0.72,-0.72,-0.72,-0.72,-0.72,-0.72,-0.72},
            {-0.64,-0.64,-0.64,-0.64,-0.64,-0.64,-0.64,-0.64,-0.64,-0.64,-0.64,-0.64,-0.64,-0.64,-0.64,-0.64,-0.64,-0.64,-0.64},
            {-0.56,-0.56,-0.56,-0.56,-0.56,-0.56,-0.56,-0.56,-0.56,-0.56,-0.56,-0.56,-0.56,-0.56,-0.56,-0.56,-0.56,-0.56,-0.56},
            {-0.48,-0.48,-0.48,-0.48,-0.48,-0.48,-0.48,-0.48,-0.48,-0.48,-0.48,-0.48,-0.48,-0.48,-0.48,-0.48,-0.48,-0.48,-0.48},
            {-0.4,-0.4,-0.4,-0.4,-0.4,-0.4,-0.4,-0.4,-0.4,-0.4,-0.4,-0.4,-0.4,-0.4,-0.4,-0.4,-0.4,-0.4,-0.4},
            {-0.32,-0.32,-0.32,-0.32,-0.32,-0.32,-0.32,-0.32,-0.32,-0.32,-0.32,-0.32,-0.32,-0.32,-0.32,-0.32,-0.32,-0.32,-0.32},
            {-0.24,-0.24,-0.24,-0.24,-0.24,-0.24,-0.24,-0.24,-0.24,-0.24,-0.24,-0.24,-0.24,-0.24,-0.24,-0.24,-0.24,-0.24,-0.24},
            {-0.16,-0.16,-0.16,-0.16,-0.16,-0.16,-0.16,-0.16,-0.16,-0.16,-0.16,-0.16,-0.16,-0.16,-0.16,-0.16,-0.16,-0.16,-0.16},
            {-0.08,-0.08,-0.08,-0.08,-0.08,-0.08,-0.08,-0.08,-0.08,-0.08,-0.08,-0.08,-0.08,-0.08,-0.08,-0.08,-0.08,-0.08,-0.08},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0.08,0.08,0.08,0.08,0.08,0.08,0.08,0.08,0.08,0.08,0.08,0.08,0.08,0.08,0.08,0.08,0.08,0.08,0.08},
            {0.16,0.16,0.16,0.16,0.16,0.16,0.16,0.16,0.16,0.16,0.16,0.16,0.16,0.16,0.16,0.16,0.16,0.16,0.16},
            {0.24,0.24,0.24,0.24,0.24,0.24,0.24,0.24,0.24,0.24,0.24,0.24,0.24,0.24,0.24,0.24,0.24,0.24,0.24},
            {0.32,0.32,0.32,0.32,0.32,0.32,0.32,0.32,0.32,0.32,0.32,0.32,0.32,0.32,0.32,0.32,0.32,0.32,0.32},
            {0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4},
            {0.48,0.48,0.48,0.48,0.48,0.48,0.48,0.48,0.48,0.48,0.48,0.48,0.48,0.48,0.48,0.48,0.48,0.48,0.48},
            {0.56,0.56,0.56,0.56,0.56,0.56,0.56,0.56,0.56,0.56,0.56,0.56,0.56,0.56,0.56,0.56,0.56,0.56,0.56},
            {0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.64,0.64},
            {0.72,0.72,0.72,0.72,0.72,0.72,0.72,0.72,0.72,0.72,0.72,0.72,0.72,0.72,0.72,0.72,0.72,0.72,0.72},
            {0.8,0.8,0.8,0.8,0.8,0.8,0.8,0.8,0.8,0.8,0.8,0.8,0.8,0.8,0.8,0.8,0.8,0.8,0.8},
            {0.88,0.88,0.88,0.88,0.88,0.88,0.88,0.88,0.88,0.88,0.88,0.88,0.88,0.88,0.88,0.88,0.88,0.88,0.88},
            {0.96,0.96,0.96,0.96,0.96,0.96,0.96,0.96,0.96,0.96,0.96,0.96,0.96,0.96,0.96,0.96,0.96,0.96,0.96},
    };


    public int mX_coords[] = {0,0,0,0};
    public int mY_coords[] = {0,0,0,0};

    static int wanted_input_width = 600;
    static int wanted_input_height = 800;


    public static void dsnt(int height, int width, float [] heatmaps, int [] X_coords, int  [] Y_coords) {
        // 1. norm the heatmaps.
        float exp_sum[] = {0,0,0,0};
        int dim = 4;
        for (int h=0; h<height; h++) {
            for (int w=0; w<width; w++) {
                heatmaps[ h * dim * width + w * dim + 0] =(float)Math.exp(heatmaps[ h * dim * width + w * dim + 0]);
                exp_sum[0] += heatmaps[ h * dim * width + w * dim + 0];

                heatmaps[ h * dim * width + w * dim + 1] =(float)Math.exp(heatmaps[ h * dim * width + w * dim + 1]);
                exp_sum[1] += heatmaps[ h * dim * width + w * dim + 1];

                heatmaps[ h * dim * width + w * dim + 2] =(float)Math.exp(heatmaps[ h * dim * width + w * dim + 2]);
                exp_sum[2] += heatmaps[ h * dim * width + w * dim + 2];

                heatmaps[ h * dim * width + w * dim + 3] =(float)Math.exp(heatmaps[ h * dim * width + w * dim + 3]);
                exp_sum[3] += heatmaps[ h * dim * width + w * dim + 3];

            }
        }

        for (int d=0; d<dim; d++) {
            exp_sum[d] = (float)Math.max(exp_sum[d], 1e-12);
        }

        for (int h=0; h<height; h++) {

            for (int w=0; w<width; w++) {

                heatmaps[h * dim * width + w * dim]/=exp_sum[0];
                heatmaps[h * dim * width + w * dim + 1]/=exp_sum[1];
                heatmaps[h * dim * width + w * dim + 2]/=exp_sum[2];
                heatmaps[h * dim * width + w * dim + 3]/=exp_sum[3];
            }
        }

        // 2. coordinate transform
        float x_coords[] = {0,0,0,0};
        float y_coords[] = {0,0,0,0};

        for (int h=0; h<height; h++) {

            for (int w=0; w<width; w++) {
                float x_element = (float)X[h][w];
                float y_element = (float)Y[h][w];
                double _ele [] = {0,0,0,0};
                _ele[0] = heatmaps[h * width * dim +  w*dim];
                _ele[1] = heatmaps[h * width * dim +  w*dim + 1];
                _ele[2] = heatmaps[h * width * dim +  w*dim + 2];
                _ele[3] = heatmaps[h * width * dim +  w*dim + 3];

                for (int d=0; d<dim; d++) {
                    x_coords[d] += (_ele[d] * x_element);
                    y_coords[d] += (_ele[d] * y_element);
                }
            }
        }


        for (int d=0; d<dim; d++) {
            float x_coord = x_coords[d];
            float y_coord = y_coords[d];

            int x_real_coord = (int)Math.round( (x_coord+1)/2.0 * wanted_input_width);
            int y_real_coord = (int)Math.round( (y_coord+1)/2.0 * wanted_input_height);

            X_coords[d] = x_real_coord;
            Y_coords[d] = y_real_coord;
        }


//        0:array([45, 56])
//        1:array([494,  50])
//        2:array([ 47, 768])
//        3:array([543, 751])
    }


}
